<?php

/* @var $this yii\web\View */

use yii\helpers\Html;
use yii\helpers\Url;
use yii\widgets\ActiveForm;
use app\assets\SmartWizardAsset;
SmartWizardAsset::register($this);

$this->title = '充值';
$this->params['breadcrumbs'][] = $this->title;
?>
<!-- page title -->
<div class="page-title">
  <div>
    <h1><i class="fa fa-trophy"></i> 充值</h1>
  </div>
</div>
<!-- page title end-->

<!-- page body start -->
<div class="card">
  <div id="wizard" class="form_wizard wizard_horizontal">
    <ul class="wizard_steps">
      <li>
        <a href="#step-1">
          <span class="step_no">1</span>
          <span class="step_descr">
                            第一步<br />
                            <small>选择通道</small>
                        </span>
        </a>
      </li>
      <li>
        <a href="#step-2">
          <span class="step_no">2</span>
          <span class="step_descr">
                            第二步<br />
                            <small>设置金额</small>
                        </span>
        </a>
      </li>
      <li>
        <a href="#step-3">
          <span class="step_no">3</span>
          <span class="step_descr">
                            第三步<br />
                            <small>充值</small>
                        </span>
        </a>
      </li>
      <li>
        <a href="#step-4">
          <span class="step_no">4</span>
          <span class="step_descr">
                            第四步<br />
                            <small>完成</small>
                        </span>
        </a>
      </li>
    </ul>
    <div id="step-1">
    </div>
    <div id="step-2">
    </div>
    <div id="step-3">
    </div>
    <div id="step-4">
    </div>

  </div>
</div>
<!-- page body end-->


<!-- Modal -->
<!--div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
  <div class="modal-dialog modal-sm" role="document">
    <div class="modal-content">
      <div class="modal-header btn-warning">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
        <h4 class="modal-title" id="myModalLabel"><i class="fa fa-key"></i> 已完成充值</h4>
      </div>
      <div class="modal-body">
        <h3>已完成充值</h3>
      </div>
      <div class="modal-footer">
        <button id='finish_congzhi' type="button" class="btn btn-warning"  data-dismiss="modal">关闭</button>
      </div>
    </div>
  </div>
</div-->

<?php $this->beginBlock('wizard-js') ?>
$(document).ready(function() {
  if( typeof ($.fn.smartWizard) === 'undefined'){ return; }
  var tokenName = '<?=Yii::$app->getRequest()->csrfParam?>';
  var tokenVal = '<?=Yii::$app->getRequest()->getCsrfToken()?>';
  var $this=this;
  $('#wizard').smartWizard({
    contentURL:'<?=Url::toRoute('site/wizardcongzhiajax')?>',
    contentCache:false,
    labelNext:'下一步',
    labelPrevious:'上一步',
    labelFinish:'完成',
    onLeaveStep:function($step,context){
      if(context.toStep>context.fromStep){
      }else if(context.toStep < context.fromStep){
      }
      return true;
    },
    onShowStep:function($step,context){
      if(context.toStep===3){
        $('.buttonNext').text('立即充值');
      }else{
        $('.buttonNext').text('下一步');
      }
      if(context.toStep===4){
        $('.buttonNext').hide();
        $('.buttonPrevious').hide();
        $('.buttonFinish').removeClass('.btn-default').addClass('.btn-info');

        var paychannel = $('#deal-channel').attr('channel');
        if(paychannel==1){
          $('#easepay').submit();
        }
        if((paychannel==1)||(paychannel==3)){
          //console.log($('#order-no').text());
          var order_no=$('#order-no').text();

          var query_result_timer = function()
          {
            $.ajax({
              url:"<?=Url::toRoute('pay/querycongzhiresultajax')?>",
              type:"get",
              datatype:"json",
              cache:false,
              data:{
                orderno:order_no
              },
              success:function(data){
                 var obj = JSON.parse(data);
                 if(obj.errcode==0){
                   $('#result-text').text('充值成功。');
                   $('#congzhi-result').modal();
                     //console.log('充值成功。5秒后跳转');
                   //自动跳转
                   setTimeout(
                   function () {
                     $this.location.href = "<?=Url::toRoute('site/wizardcongzhi')?>"
                   }, 5000);
                 }else if(obj.errcode==1){
                   $('.fa-exclamation').removeClass('alert-info').addClass('alert-danger');
                   $('#result-text').text('充值失败。');
                   $('#congzhi-result').modal();
                   //console.log('充值失败。5秒后跳转');
                   //自动跳转
                   setTimeout(
                   function () {
                     $this.location.href = "<?=Url::toRoute('site/wizardcongzhi')?>"
                   }, 5000);
                 }else if(obj.errcode==2){
                   //console.log('等待结果...');
                   //循环等待
                   setTimeout(
                   query_result_timer, 1000);
                 }else if(obj.errcode==3){
                   $('.fa-exclamation').removeClass('alert-info').addClass('alert-danger');
                   $('#result-text').text(obj.errmsg);
                   $('#congzhi-result').modal();
                   //console.log('发生错误。5秒后跳转');
                   //自动跳转
                   setTimeout(
                   function () {
                     $this.location.href = "<?=Url::toRoute('site/wizardcongzhi')?>"
                   }, 5000);
                 }
              },
              error:function(jqXHR,textStatus,errorThrown){
                console.log(jqXHR);
                console.log(textStatus);
                console.log(errorThrown);
                $('.compose').slideToggle();
              }
            });
          };
          //console.log('首次执行结果查询');
          query_result_timer();
        }

      }
      if(context.toStep===2)
      {
        //$('#congzhimoneyform-money').val(10.00);
        //this.curStepIdx=2;
        //this.goToStep(3);
        //this.goForward();
      }
      return true;
    },
    onFinish:function($step, context){
      //alert('完成充值,返回充值开始页面');
      window.location.href='<?=Url::toRoute('site/wizardcongzhi')?>';
      return true;
    },
    contentURLData:function(stepNum){

      switch(stepNum){
        //第一步：设置通道
        case 1:
          var obj= {
            data:{
              step_number : stepNum
            }
          };
          obj.data[tokenName]=tokenVal;
          return obj;
        break;
        //设置金额
        case 2:
        var obj= {
          data:{
            step_number : stepNum,
            channel: $('#congzhichannelform-channel option:selected').val()
          }
        };
        obj.data[tokenName]=tokenVal;
          return obj;
        break;
        //第三步：验证
        case 3:
        var obj= {
          data:{
            step_number : stepNum,
            channel:$('#congzhimoneyform-channel').val(),
            money:$('#congzhimoneyform-money').val()
          }
        };
        obj.data[tokenName]=tokenVal;
          return obj;
        break;
        //第四步：扫码（支付宝，微信）,网银
        case 4:
        var obj= {
          data:{
            step_number : stepNum,
            channel:$('#congzhimoneyform-channel').val(),
            money:$('#congzhimoneyform-money').val()
          }
        };
        obj.data[tokenName]=tokenVal;
          return obj;
          
        break;
        default:
      }
    }
  });

  $('.buttonNext').addClass('btn btn-warning');
  $('.buttonPrevious').addClass('btn btn-warning');
  $('.buttonFinish').addClass('btn btn-default');
});
<?php $this->endBlock() ?>
<?php $this->registerJs($this->blocks['wizard-js'],\yii\web\View::POS_END); ?>
